package ua.maxicash.editors;

import ua.maxicash.service.AppService;
import ua.maxicash.vo.SystemUser;

import java.beans.PropertyEditorSupport;

public class SystemUserEditor extends PropertyEditorSupport {

    //@Autowired
    AppService appService;

    public SystemUserEditor(AppService appService) {
        this.appService=appService;
    }

    @Override
    public String getAsText() {

        SystemUser su = (SystemUser) getValue();
        if (su != null) {
            return su.getEmail();
        } else {
            return "";
        }
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        SystemUser su = appService.getSystemUserByEmail(text);
        if (su != null) {
            setValue(su);
        } else {
            throw new IllegalArgumentException();
        }

    }
}